package com.huawei.appgallery.updatemanager.impl.store.bean;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.text.TextUtils;
import com.huawei.appgallery.foundation.store.bean.spilt.DeviceSpec;
import com.huawei.appgallery.foundation.store.bean.spilt.DeviceSpec.e;
import com.huawei.appgallery.jsonkit.api.JsonBean;
import com.huawei.appgallery.updatemanager.api.UpgradeResponse;
import com.huawei.appgallery.updatemanager.impl.fileinfo.dao.AppFileInfo;
import com.huawei.fastengine.BuildConfig;
import com.huawei.hwid.core.constants.HwAccountConstants;
import com.tencent.mm.opensdk.constants.ConstantsAPI.AppSupportContentFlag;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import o.cep;
import o.cfw;
import o.cvs;
import o.czp;
import o.dam;
import o.das;
import o.dbn;
import o.dnk;
import o.doa;
import o.dok;
import o.ContentManager;
import o.eux;

public class UpgradeRequest extends cvs {
    public static final String APIMETHOD = "client.diffUpgrade2";
    public static final String APIMETHOD2 = "client.singleAppUpgrade";
    public static final String APIMETHOD3 = "client.oneAppNonServiceTypeUpgrade";
    public static final int DEFAULT_UPGRADE_RESULT = 0;
    public static final int FULL_UPGRADE_RESULT = 1;
    private static final int PRE_AUTOUPDATE_OPEN = 2;
    private static final int PRE_DOWNLOAD_CLOSE = 0;
    private static final String TAG = "UpgradeRequest";
    private DeviceSpec deviceSpecParams_;
    private int installCheck_ = 0;
    private int isFullUpgrade_ = 0;
    private int isWlanIdle_ = 0;
    private Json json_;
    private String maxMem_;

    public static class Json extends JsonBean {
        private List<Param> params_;

        public void ˊ(List<Param> list) {
            this.params_ = list;
        }
    }

    public static class Param extends JsonBean {
        private String fSha2_;
        private int isPre_;
        private List<String> keySets_;
        private int maple_;
        private String oldVersion_;
        private String package_;
        private String sSha2_;
        private int targetSdkVersion_;
        private int versionCode_;

        public Param(PackageInfo packageInfo, boolean z) {
            this.package_ = packageInfo.packageName;
            this.versionCode_ = packageInfo.versionCode;
            this.oldVersion_ = packageInfo.versionName == null ? HwAccountConstants.NULL : packageInfo.versionName;
            this.targetSdkVersion_ = packageInfo.applicationInfo.targetSdkVersion;
            this.isPre_ = cep.ˎ(packageInfo);
            ॱ(packageInfo);
            ˋ(packageInfo);
            das.ˎ();
            this.keySets_ = das.ˋ(packageInfo);
            this.maple_ = ˎ(packageInfo);
        }

        private void ˋ(PackageInfo packageInfo) {
            if (packageInfo.signatures != null) {
                Object ॱ = eux.ॱ(dnk.ˏ(doa.ˊ(packageInfo.signatures[0].toCharsString())));
                if (!TextUtils.isEmpty(ॱ)) {
                    this.sSha2_ = ॱ.toLowerCase(Locale.getDefault());
                }
            }
        }

        private void ॱ(PackageInfo packageInfo) {
            AppFileInfo ˏ = dam.ˏ(this.package_, this.versionCode_);
            if (packageInfo.applicationInfo.sourceDir != null) {
                File file = new File(packageInfo.applicationInfo.sourceDir);
                if (ˏ != null && file.lastModified() == ˏ.ˊ() && !TextUtils.isEmpty(ˏ.ˋ())) {
                    this.fSha2_ = ˏ.ˋ();
                }
            }
        }

        private int ˎ(PackageInfo packageInfo) {
            if (packageInfo == null || packageInfo.applicationInfo == null || packageInfo.applicationInfo.metaData == null || !packageInfo.applicationInfo.metaData.containsKey("com.huawei.maple.flag")) {
                return 0;
            }
            return 1;
        }
    }

    static {
        cfw.ˊ(APIMETHOD, UpgradeResponse.class);
        cfw.ˊ(APIMETHOD2, UpgradeResponse.class);
        cfw.ˊ(APIMETHOD3, UpgradeResponse.class);
    }

    public UpgradeRequest() {
        ˎ();
    }

    public void ˊ(int i) {
        this.installCheck_ = i;
    }

    public void ˊ(Json json) {
        this.json_ = json;
    }

    public void ˊ(String str) {
        this.maxMem_ = str;
    }

    public void ˎ(int i) {
        this.isWlanIdle_ = i;
    }

    public static UpgradeRequest ˏ(Context context, String str, int i) {
        PackageInfo packageInfo = new PackageInfo();
        packageInfo.packageName = str;
        packageInfo.versionName = BuildConfig.VERSION_NAME;
        packageInfo.versionCode = 1;
        ApplicationInfo applicationInfo = new ApplicationInfo();
        applicationInfo.targetSdkVersion = 19;
        packageInfo.applicationInfo = applicationInfo;
        List arrayList = new ArrayList();
        arrayList.add(packageInfo);
        return ˋ(context, arrayList, false, i);
    }

    public static UpgradeRequest ˋ(Context context, List<PackageInfo> list, boolean z, int i) {
        UpgradeRequest upgradeRequest = new UpgradeRequest();
        upgradeRequest.setStoreApi("clientApi");
        if ((i & 1) != 0) {
            upgradeRequest.setMethod_(APIMETHOD2);
        } else if ((i & 2) != 0) {
            upgradeRequest.setMethod_(APIMETHOD3);
        } else {
            upgradeRequest.setMethod_(APIMETHOD);
        }
        upgradeRequest.ˊ(String.valueOf(dok.ˋ(context) / AppSupportContentFlag.MMAPP_SUPPORT_XLS));
        upgradeRequest.setVer_("1.2");
        upgradeRequest.ˎ(((czp) dbn.ˎ(czp.class)).ˎ(context) ? 2 : 0);
        Json json = new Json();
        upgradeRequest.ˊ(json);
        List arrayList = new ArrayList();
        json.ˊ(arrayList);
        for (PackageInfo param : list) {
            arrayList.add(new Param(param, z));
        }
        return upgradeRequest;
    }

    private void ˎ() {
        this.deviceSpecParams_ = new e(ContentManager.getInstant().getContext()).ˎ(true).ॱ();
    }
}
